--
-- NewDungeonTypeRender.lua
-- Created by IntelliJ IDEA.
-- User: yao.chen
-- Date: 2019/1/7
-- 地下城入口render
--

local ClassRef = app.mvc.createRenderClass(...)

local ENABLED_COLOR = cc.c3b(239, 213, 157)
local DISABLED_COLOR = cc.c3b(255, 232, 202)

--data =
function ClassRef.createWithData(data, node)
    local sprite = ClassRef.extend(node or cc.CSLoader:createNode(Res.NewTeamSceneTypeNode))
    if node == nil then
        ccui.Helper:doLayout(sprite)
    end
    sprite:init(data)
    return sprite
end

function ClassRef.create(node)
    return ClassRef.createWithData(nil, node)
end

function ClassRef:init(data)
    self.super.init(self, data)
    self:setContentSize(self._ui.Panel:getContentSize())

    self._img = self._ui["Panel/img_1"]

    display.uiAddClick(self._ui["Panel"], function(sender, eventType)
        if self._type == Enums.TeamTargetType.Dungeon then
            if self._data.enable == "1" then
                executeFunc(self._onClick, "dungeonsType_selected" , tonumber(self._data.ID))
            else
                display.pushToast(L("lua_code_text_458"))
            end
        elseif self._type == Enums.TeamMatchType.Pvp then
            executeFunc(self._onClick, self)
        end
    end, true, 0.02)

    self._DungeonPanel = self._ui["Panel/Panel_dungeons"]
    self._DungeonPanel:setVisible(false)

    self._ChampionPanel = self._ui["Panel/Panel_champion"]
    self._ChampionPanel:setVisible(false)

    self._title = self._ui["Panel/tlt"]

    self._panelMark = self._ui["Panel/Panel_mark"]

    self._isOpen = false
end

-- type Enums.TeamTargetType
-- openLv 开放等级
function ClassRef:setType(type, openLv)
    self._type = type
    self._openLv = openLv
end

function ClassRef:refresh()
    if not self._data then return end
    local conf = self._data
    local bgPath
    local isOpen = (self._openLv <= UD:getLevel())
    self._isOpen = isOpen

    if self._type == Enums.TeamTargetType.Dungeon then
        self._DungeonPanel:setVisible(true)
        local dungeon_tlt = self._DungeonPanel:getChildByName("tlt")
        local dungeon_text = self._DungeonPanel:getChildByName("text")

        dungeon_tlt:setString(L(conf.name))
        dungeon_text:setString(L(conf.desc))
        if isOpen then
            dungeon_tlt:setTextColor(ENABLED_COLOR)
            dungeon_text:setTextColor(ENABLED_COLOR)
        else
            dungeon_tlt:setTextColor(DISABLED_COLOR)
            dungeon_text:setTextColor(DISABLED_COLOR)
        end
    elseif self._type == Enums.TeamMatchType.Pvp then
        self._ChampionPanel:setVisible(true)
        local champion_tlt = self._ChampionPanel:getChildByName("tlt")
        local champion_text = self._ChampionPanel:getChildByName("text")

        champion_tlt:setString(L(conf.name))
        champion_text:setString(L(conf.desc))
        if isOpen then
            champion_tlt:setTextColor(ENABLED_COLOR)
            champion_text:setTextColor(ENABLED_COLOR)
        else
            champion_tlt:setTextColor(DISABLED_COLOR)
            champion_text:setTextColor(DISABLED_COLOR)
        end
    end

    -- common
    local showNum = tonumber(conf.showNum)
    if not TEAM_LEGION_ENABLE then
        -- 不允许10人团本，最高只有5人
        showNum = math.min(showNum, 5)
    end
    self._panelMark:findChild("num"):setString(string.format(L("lua_code_text_318"), showNum))
    if isOpen then
        self._panelMark:setVisible(true)
        bgPath = GD:getImagePath(Res.teamtarget_pic, conf.banner)

        self._title:setVisible(false)
    else
        self._panelMark:setVisible(false)
        bgPath = GD:getImagePath(Res.teamtarget_pic, conf.unopenbanner)

        if self._openLv and conf.enable == "1" then
            self._title:setString(string.format(L("openLvShow"), self._openLv))
            self._title:setVisible(true)
        else
            self._title:setVisible(false)
        end
    end

    if bgPath then
        self._img:loadTexture(bgPath)
    end
end

function ClassRef:setFirstWin(b)
    self._ui.first:setVisible(self._isOpen and b or false)
end

function ClassRef:onBtnClick(cb)
    self._onClick = cb
end

return ClassRef
